package com.freewater.springboot.demo.aop.introduction;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.DeclareParents;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class UsageTrackingAspect {

    @DeclareParents(value = "com.freewater.springboot.demo.aop.introduction.BusinessService+", defaultImpl = DefaultUsageTracked.class)
    public static UsageTracked mixin1;

    @DeclareParents(value = "com.freewater.springboot.demo.aop.introduction.OtherService+", defaultImpl = DefaultUsageTracked.class)
    public static UsageTracked mixin2;

    @Pointcut(value = "execution(public * com.freewater.springboot.demo.aop.introduction.*Service+.*(..)) && this(usageTracked)", argNames = "usageTracked")
    public void allPublicMethods(UsageTracked usageTracked) {

    }

    @Before(value = "allPublicMethods(usageTracked)", argNames = "joinpoint,usageTracked")
    public void recordUsage(JoinPoint joinpoint, UsageTracked usageTracked) {
        usageTracked.increaseUseCount(joinpoint.getTarget());
    }
}
